package com.zhoug.common2.utils;

import android.app.ActivityManager;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ApplicationInfo;
import android.content.res.Resources;
import android.os.Build;
import android.os.Process;

import com.zhoug.logger2.Logger;

import java.util.List;

/**
 * @Author: zhoug
 * @Date: 2024-11-16
 * @Description:
 */
public class AppUtils {
    private static final String TAG = ">>>AppUtils";
    private static final boolean DEBUG=false;
    /**
     * 获取状态栏的高度
     * {@link @android:dimen/status_bar_height}
     *
     * @return
     */
    public static int getStatusHeight(Context context) {
        Resources resources =context.getResources();
        int resourceId = resources.getIdentifier("status_bar_height", "dimen", "android");

        int status_bar_height = resources.getDimensionPixelSize(resourceId);
        if (DEBUG) {
            Logger.d(TAG, "getStatusHeight:status_bar_height" + status_bar_height);
        }
        return status_bar_height;
    }

    /**
     * 获取进程名称
     *
     * @param context
     * @return
     */
    public static String getProcessName(Context context) {
        String processName = null;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
            processName = Process.myProcessName();
        }
        if (processName == null) {
            if (context != null) {
                ApplicationInfo applicationInfo = context.getApplicationInfo();
                processName = applicationInfo != null ? applicationInfo.processName : null;
            }
        }
        return processName;
    }

    public static boolean isActivityLaunched(Context context, String className) {
        try {
            Class<?> aClass = context.getClassLoader().loadClass(className);
            return isActivityLaunched(context, aClass);
        } catch (Exception e) {
            Logger.e(TAG, e.getMessage());
        }
        return false;
    }


    /**
     * 判断给定的activity是否启动
     * 5.0以后被废弃
     *
     * @param context       上下文
     * @param activityClass activity类
     */
    public static boolean isActivityLaunched(Context context, Class<?> activityClass) {
        Intent intent = new Intent(context, activityClass);
        ComponentName cmpName = intent.resolveActivity(context.getPackageManager());

        if (cmpName != null) {
            ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
            List<ActivityManager.RunningTaskInfo> taskInfoList = am.getRunningTasks(10);
            for (ActivityManager.RunningTaskInfo taskInfo : taskInfoList) {
                if (taskInfo.baseActivity != null && taskInfo.baseActivity.equals(cmpName)) {
                    Logger.d(TAG, "RunningTasks:" + taskInfo.baseActivity.getClassName());
                    return true;
                }
            }
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                List<ActivityManager.AppTask> appTasks = am.getAppTasks();
                if (appTasks != null && !appTasks.isEmpty()) {
                    for (ActivityManager.AppTask appTask : appTasks) {
                        ActivityManager.RecentTaskInfo taskInfo = appTask.getTaskInfo();
                        if (taskInfo.baseActivity != null && taskInfo.baseActivity.equals(cmpName)) {
                            Logger.d(TAG, "appTasks:" + taskInfo.baseActivity.getClassName());
                            return true;
                        }
                    }
                }
            }

        }
        return false;
    }


}
